package com.phison.fenestrapro.dashboard.ui;

import org.vaadin.cssinject.CSSInject;

import com.phison.fenestrapro.dashboard.FenestraProApplication;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;

/**
 *
 * 
 * @author Niall O'Hara
 *
 */
public class LoginWindow extends Window
{

	private static final long serialVersionUID = 1L;
	
	private Button btnLogin = new Button("Login");
    private TextField username = new TextField ( "Username" );
    private  PasswordField  password = new  PasswordField  ( "Password" );
    
    public LoginWindow ()
    {
        super("Login | FenestraPro");
        setName("Login");
        initUI();
    }

    @SuppressWarnings("serial")
	private void initUI()
    {
    	
    	CSSInject css = new CSSInject();
    	css.addStyleSheet(new ThemeResource("login.css"));
    	
    	AbsoluteLayout vp = new AbsoluteLayout();
    	vp.setSizeFull();
    	
        vp.addComponent(css);
        
    	CustomLayout custom = new CustomLayout("loginpage");
    	custom.setSizeFull();
   
    	vp.addComponent(custom);
    	
        setContent(vp);

        username = new TextField();
    	custom.addComponent(username, "login_username");
    	username.setStyleName("loginform");
    	username.setDebugId("login_username");
    	 
    	password = new PasswordField();
    	custom.addComponent(password, "login_password");
    	password.setStyleName("loginform");
    	password.setDebugId("login_password");
    	 
    	btnLogin = new Button("Login");
    	custom.addComponent(btnLogin, "btnLogin");
    	btnLogin.setDebugId("login_password");
        
    	
    	btnLogin.addListener ( new Button.ClickListener()
        {
			public void buttonClick ( Button.ClickEvent event )
            {
                try
                {
                	FenestraProApplication.getInstance().authenticate((String)username.getValue(), (String)password.getValue());
                    open( new ExternalResource (FenestraProApplication.getInstance().getURL()));
                }
                catch ( Exception e )
                {
                    showNotification(e.getMessage());
                }
            }
        });
        
    } 
}

